local skill = fk.CreateSkill {
  name = "3s__zhubao",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable{
  ["3s__zhubao"] = "诛暴",
  [":3s__zhubao"] = "限定技，当你对一名角色造成伤害时，你可将伤害值改为X点（X为其本局单回合内造成过的最大伤害数）。",
  ["$3s__zhubao1"] = "诛暴除乱，以安天下！",
  ["$3s__zhubao2"] = "暴虐当诛，不容姑息！",
  ["#3s__zhubao"] = "诛暴：是否将 %dest 受到的伤害改为 %arg 点？",
}

skill:addEffect(fk.DamageCaused, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skill.name) and data.to and
    player:usedSkillTimes(skill.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    -- 获取目标角色本局单回合内造成过的最大伤害数
    local max_damage = 0
    local to = data.to
    -- 使用getActualDamageEvents获取目标角色造成的所有伤害事件
    room.logic:getActualDamageEvents(nil, function(e)
      local damage = e.data
      if damage.from and damage.from == to and damage.damage > 0 then
        -- 获取这个伤害事件所在的回合
        local turn_event = e:findParent(GameEvent.Turn, true)
        if turn_event then
          -- 计算这个回合内目标角色造成的总伤害
          local turn_damage = 0
          room.logic:getActualDamageEvents(nil, function(turn_e)
            local turn_damage_data = turn_e.data
            if turn_damage_data.from and turn_damage_data.from == to and
               turn_e:findParent(GameEvent.Turn, true) == turn_event then
              turn_damage = turn_damage + turn_damage_data.damage
              return false -- 继续查找
            end
          end, Player.HistoryGame)
          max_damage = math.max(max_damage, turn_damage)
        end
        return false -- 继续查找其他伤害事件
      end
    end, Player.HistoryGame)
    -- 如果最大伤害数大于0且与当前伤害不同，则询问是否发动
    if max_damage > 0 and max_damage ~= data.damage then
      -- 记录最大伤害数
      event:setCostData(self, { max_damage = max_damage })
      local prompt = "#3s__zhubao::"..to.id..":"..max_damage
      return room:askToSkillInvoke(player, {
        skill_name = skill.name,
        prompt = prompt,
      })
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local max_damage = event:getCostData(self).max_damage
    -- 修改伤害值
    data.damage = max_damage
  end,
})

return skill